import openpyxl
import xlwt
import glob
import os
import tqdm

def convert_xlsx_to_xls(input_file, output_file):
    wb = openpyxl.load_workbook(input_file)
    sheet_names = wb.sheetnames
    xls_file = xlwt.Workbook()

    for sheet_name in sheet_names:
        xls_sheet = xls_file.add_sheet(sheet_name)
        ws = wb[sheet_name]

        for r, row in enumerate(ws.iter_rows()):
            for c, cell in enumerate(row):
                xls_sheet.write(r, c, cell.value)

    xls_file.save(output_file)

def main():
    files = glob.glob(r'D:\xlsx文件夹\*\*.xlsx')  # xlsx文件夹的所有子文件夹下的.xlsx文件
    # files = glob.glob(r'D:\xlsx文件夹\*.xlsx')  # xlsx文件夹下所有.xlsx文件

    pbar = tqdm.tqdm(total=len(files))
    for file in files:
        if '~$' in file:  # xlsx缓存文件（windows系统）
            continue
        if not os.path.exists('output'):
            os.mkdir('output')
        output = 'output\\' + '.'.join(os.path.basename(file).split('.')[:-1]) + '.xls'
        convert_xlsx_to_xls(file, output)
        pbar.update(1)

if __name__ == '__main__':
    main()
